package com.example.demo.controller;

import com.example.demo.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;

@Api(value = "用户模块")
@RestController
public class UserController {
    //模拟数据库
    public static ArrayList<User> users = new ArrayList<>();

    static {
        users.add(new User(1L, "zhangsan", 18));
        users.add(new User(2L, "lisi", 19));
    }

    @ApiOperation(value = "获取用户列表", notes = "获取所有用户的列表", httpMethod = "GET")
    @GetMapping("/users")
    public Object index() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("users", users);
        return users;
    }

    @ApiOperation(value = "根据id查询用户", notes = "查询单个信息", httpMethod = "GET")
    @GetMapping("/user/{id}")
    public Object getUserById(@PathVariable("id") String id) {
        int uId = Integer.parseInt(id) - 1;
        User user = users.get(uId);
        return user;
    }

    @ApiOperation(value = "根据id查询用户", notes = "查询单个信息", httpMethod = "GET")
    @GetMapping("/hello/{id}")
    public Object hello(@PathVariable("id") String id) {
        int uId = Integer.parseInt(id) - 1;
        User user = users.get(uId);

        ModelAndView mv = new ModelAndView();
        mv.addObject("username",user.getUsername());
        mv.setViewName("hello");

        return mv;
    }
}
